package com.thinkgem.jeesite.modules.data.ftp.service;

import com.thinkgem.jeesite.common.persistence.BaseEntity;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.modules.data.ftp.dao.TfFileIoConfDao;
import com.thinkgem.jeesite.modules.data.ftp.entity.TfFileIoConf;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * Ftp 文件接口配置化服务
 * Created by Havel on 2019/5/14/014.
 */
@Service
@Transactional(readOnly = true)
public class FtpFileIoService  {

    @Autowired
    TfFileIoConfDao fileIoConfDao;

    public int insertFileData2DB(TfFileIoConf fileIoConf) {

        List<Map<String, Object>> insertMaps = fileIoConf.getInsertMaps();
        int countInterval = fileIoConf.getBatRowNum();//countInterval = 0将死循环
        if(countInterval == 0){
            countInterval = 5000;
        }
        int preCount = 0;
        int total = 0;
        for (int i = countInterval; i < insertMaps.size()+countInterval; i = (i+countInterval)) {
            List<Map<String, Object>> tmpMaps = insertMaps.subList(preCount, (i < insertMaps.size()) ? i : insertMaps.size());
            fileIoConf.setInsertMaps(tmpMaps);
            preCount = i;
            total = total + (TfFileIoConf.IS_YES.equals(fileIoConf.getIsSeq()) ? fileIoConfDao.insertFileDate2BatchWithSeq(fileIoConf) : fileIoConfDao.insertFileDate2Batch(fileIoConf));
        }
        return total;
    }

    public List<Map<String, Object>> findList2FtpFile4Default(TfFileIoConf ioConf) {
        return fileIoConfDao.selectDbDataDefault(ioConf);
    }

    public Page<Map<String, Object>> findList2FtpFile4DefaultPage(Page<Map<String, Object>> page, TfFileIoConf ioConf) {
        page.setList(fileIoConfDao.selectDbDataDefault(ioConf));
        return page;
    }


    public List<Map<String, Object>> findList2FtpFile4Reflect(TfFileIoConf ioConf) {
        fileIoConfDao.selectByPrimaryKey(ioConf.getConfId());
        //ReflectionUtils
        return null;
    }

    public List<Map<String, Object>> findList2FtpFile4ConfSql(TfFileIoConf ioConf) {
        return null;
    }

    /**
     * 提交或修改
     */
    public int saveAndUpdate(TfFileIoConf ioConf) {
        Date date = new Date();
        if (StringUtils.isEmpty(ioConf.getConfId())) {
            ioConf.setCreateTime(date);
            ioConf.setUpdateTime(date);
            //ioConf.setInStaffId(configOut.getUpdateStaffId());
            ioConf.setDelFlag(BaseEntity.DEL_FLAG_NORMAL);
            return fileIoConfDao.insertSelective(ioConf);
        } else {
            ioConf.setUpdateTime(date);
            return fileIoConfDao.updateByPrimaryKeySelective(ioConf);
        }
    }

    public int delete(TfFileIoConf ioConf) {
        return 0;
    }

    public List findList(TfFileIoConf ioConf) {
        return null;
    }

    public Page findPage(Page<TfFileIoConf> page, TfFileIoConf ioConf) {
        return null;
    }

    public TfFileIoConf getIoConfById(Long id) {
        return  fileIoConfDao.selectByPrimaryKey(id);
    }

}
